#include "SampleBoard.h"
#include "EventReciever.h"

SampleBoard::SampleBoard(int set, float x, float y)
{
	m_bPlaying=false;
	m_bLastPlaying=false;
	m_bPaused=false;
	m_bLooping=false;
	m_bLPF=false;
	m_bHPF=false;
	m_bMute=false;
	m_pSample=NULL;
	m_IdRangeStart = set;
	m_vPos.x=0;
	m_vPos.y=0;
	m_vPos.z=0;
	s32 me = m_IdRangeStart/100;
	char other[32]= "Sample";
	char str[3];
	sprintf_s(str, "%d", me);
	strcat_s(other, str);
	m_SampleName=other;
	IGUIButton* button;
	TheAudio::Instance()->GetSystem()->createDSPByType(FMOD_DSP_TYPE_HIGHPASS,&m_pDsp1);
	TheAudio::Instance()->GetSystem()->createDSPByType(FMOD_DSP_TYPE_LOWPASS,&m_pDsp2);
	TheAudio::Instance()->GetSystem()->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT,&m_pPitchShifter);
	TheAudio::Instance()->GetSystem()->createDSPByType(FMOD_DSP_TYPE_REVERB,&m_pReverb);
	m_pChannel->addDSP(m_pDsp1, 0);
	m_pChannel->addDSP(m_pDsp2, 0);
	m_pChannel->addDSP(m_pPitchShifter, 0);
	m_pChannel->addDSP(m_pReverb, 0);
	m_pDsp1->setBypass(true);
	m_pDsp2->setBypass(true);

	
	FMOD_VECTOR  listenerpos  = { 0.0f, 0.0f, -1.0f };
	TheAudio::Instance()->GetSystem()->set3DListenerAttributes(0, &listenerpos, 0, 0, 0);
	//sample
	m_pFileDisplay = TheEngine::Instance()->GetGUI()->addStaticText(m_wFile.c_str(),rect<s32>((set+10)*x,18*y,(set+90)*x,18*y+8*y),true,false,0,set,true);
	TheEngine::Instance()->GetGUI()->addButton(rect<s32>((set+25)*x,30*y,(set+75)*x,30*y + 8*y), 0, set+SAMPLE_LOAD_BUTTON,
		L"Load", L"Load sample");

	button = TheEngine::Instance()->GetGUI()->addButton(rect<s32>((set+ 80)*x,30*y,(set+100)*x,30*y+16),0,set+SAMPLE_MUTE_BUTTON,
		L"MUTE",L"Mutes this sample");
	button->setIsPushButton(true);
	m_pPlayButton = TheEngine::Instance()->GetGUI()->addButton(rect<s32>((set+0)*x,42*y,(set+100)*x,42*y + 16*y), 0, set+SAMPLE_PLAY_BUTTON,
		L"Play", L"Plays sample");
	button = TheEngine::Instance()->GetGUI()->addButton(rect<s32>((set+25)*x,62*y,(set+75)*x,62*y + 8*y), 0, set+SAMPLE_LOOP_BUTTON,
		L"Loop", L"Loop this sample");
	button->setIsPushButton(true);

	//volume
	TheEngine::Instance()->GetGUI()->addStaticText(L" V ",rect<s32>((set+25)*x,72*y,(set+35)*x,72*y+8*y),false,false,0,0,true);
	m_pVolBar =TheEngine::Instance()->GetGUI()->addScrollBar(false,rect<s32>((set+25)*x,84*y,(set+35)*x,84*y+128*y),0,set+10);
	m_pVolBar->setToolTipText(L"Volume");
	m_pVolBar->setMin(-256);
	m_pVolBar->setMax(0);
	m_pVolBar->setPos(-256);
	//distance
	TheEngine::Instance()->GetGUI()->addStaticText(L" D ",rect<s32>((set+45)*x,72*y,(set+55)*x,72*y+8*y),false,false,0,0,true);
	m_pDistanceBar = TheEngine::Instance()->GetGUI()->addScrollBar(false,rect<s32>((set+45)*x,84*y,(set+55)*x,84*y+128*y),0,set+20);
	m_pDistanceBar->setToolTipText(L"Distance");
	m_pDistanceBar->setMin(-256);
	m_pDistanceBar->setMax(0);
	m_pDistanceBar->setPos(0);
	//reverb
	TheEngine::Instance()->GetGUI()->addStaticText(L" R ",rect<s32>((set+65)*x,72*y,(set+75)*x,72*y+8*y),false,false,0,0,true);
	m_pReverbBar = TheEngine::Instance()->GetGUI()->addScrollBar(false,rect<s32>((set+65)*x,84*y,(set+75)*x,84*y+128*y),0,set+30);
	m_pReverbBar->setToolTipText(L"Reverb");
	m_pReverbBar->setMin(-256);
	m_pReverbBar->setMax(0);
	m_pReverbBar->setPos(0);

	m_pPanBar = TheEngine::Instance()->GetGUI()->addScrollBar(true,rect<s32>((set+4)*x,216*y,(set+96)*x,216*y+8*y),0,set+40);
	m_pPanBar->setToolTipText(L"Pan");
	m_pPanBar->setMin(-100);
	m_pPanBar->setMax(100);
	m_pPanBar->setPos(0);

	m_pSpeedBar = TheEngine::Instance()->GetGUI()->addScrollBar(true,rect<s32>((set+4)*x,228*y,(set+96)*x,228*y+6*y),0,set+40);
	m_pSpeedBar->setToolTipText(L"Playback speed");
	m_pSpeedBar->setMin(1);
	m_pSpeedBar->setMax(200);
	m_pSpeedBar->setPos(100);

	m_pPitchBar = TheEngine::Instance()->GetGUI()->addScrollBar(true,rect<s32>((set+4)*x,238*y,(set+96)*x,238*y+4*y),0,set+40);
	m_pPitchBar->setToolTipText(L"Frequency");
	m_pPitchBar->setMin(1);
	m_pPitchBar->setMax(200);
	m_pPitchBar->setPos(100);


	button = TheEngine::Instance()->GetGUI()->addButton(rect<s32>((set+4)*x,246*y,(set+48)*x,246*y + 16*y), 0, set+SAMPLE_HPF_BUTTON,
		L"HPF", L"High Pass Filter");
	button->setIsPushButton(true);
	button = TheEngine::Instance()->GetGUI()->addButton(rect<s32>((set+52)*x,246*y,(set+96)*x,246*y + 16*y), 0, set+SAMPLE_LPF_BUTTON,
		L"LPF", L"Low Pass Filter");
	button->setIsPushButton(true);
}

void SampleBoard::Update(float master, bool mute)
{
	if(mute)
	{
		m_pChannel->setMute(true);
	}
	else
	{
		m_pChannel->setMute(m_bMute);
	}
	m_pChannel->isPlaying(&m_bPlaying);

	m_bLastPlaying=m_bPlaying;
	if(!m_bPlaying)
	{
		m_pPlayButton->setText(L"Play");
	}
	else if(m_bPaused)
	{
		m_pPlayButton->setText(L"Resume");
	}
	else
	{
		m_pPlayButton->setText(L"Pause");
	}


	if(m_pVolBar->getPos()!=m_LastVol)
	{
		float i  = m_pVolBar->getPos();
		m_LastVol =i;

		float m = m_pVolBar->getMin();
		m /=100; // 1%
		i /= m;
		int d=i;
		wchar_t other[32]= L"Volume: ";
		wchar_t str[20];
		wsprintf(str, L"%d%%", d);
		wcscat_s(other, str);
		std::wstring v(other);
		m_pVolBar->setToolTipText(v.c_str());
		m_Vol =i/100;
	
	}
	m_pChannel->setVolume(m_Vol*master);
	if(m_pDistanceBar->getPos()!=m_LastDist)
	{
		int i  = m_pDistanceBar->getPos();
		m_LastDist =i;
		float m = m_pDistanceBar->getMin();
		m /=10; // 10%
		i /= m;
		wchar_t other[32]= L"Distance: ";
		wchar_t str[20];
		wsprintf(str, L"%d metres", i);
		wcscat_s(other, str);
		std::wstring v(other);
		m_pDistanceBar->setToolTipText(v.c_str());
		m_Dist=i;
		m_vPos.z=m_Dist;
		m_pChannel->set3DAttributes(&m_vPos,0);

	}
	if(m_pReverbBar->getPos()!=m_LastRev)
	{
		float f  = m_pReverbBar->getPos();
		m_LastRev =f;
		float m = m_pReverbBar->getMin();
		f /= m;
		wchar_t other[32]= L"Reverb: ";
		wchar_t str[20];
		int d = f*100;
		swprintf(str, L"%d%% ", d);
		wcscat_s(other, str);
		std::wstring v(other);
		m_pReverbBar->setToolTipText(v.c_str());
		m_Rev =f;
		m_pReverb->setParameter(0,f);

	}
	if(m_pSpeedBar->getPos()!=m_LastSpeed)
	{
		float f  = m_pSpeedBar->getPos();
		m_LastSpeed =f;
		f /=100;
		wchar_t other[32]= L"Speed: ";
		wchar_t str[20];
		swprintf(str, L"%3.2f", f);
		wcscat_s(other, str);
		std::wstring v(other);
		m_pSpeedBar->setToolTipText(v.c_str());

		m_Speed=f;
		if(m_pSample)
		{
			m_pSample->setMusicSpeed(m_Speed);
		}
	}
	if(m_pPitchBar->getPos()!=m_LastPitch)
	{
		float f  = m_pPitchBar->getPos();
		m_LastPitch =f;
		f/=100;
		wchar_t other[32]= L"Pitch Modifier: ";
		wchar_t str[20];
		swprintf(str, L"%3.2f", f);
		wcscat_s(other, str);
		std::wstring v(other);
		m_pPitchBar->setToolTipText(v.c_str());
		m_Pitch=f;
		//m_pChannel->setFrequency(m_Pitch*m_Freq);
		m_pPitchShifter->setParameter(0,m_Pitch);
	}
	if(m_pPanBar->getPos()!=m_LastPan)
	{
		float i  = m_pPanBar->getPos();
		m_LastPan =i;
		i/=10;
		wchar_t other[32]= L"Pan adjustment: ";
		wchar_t str[20];
		swprintf(str, L"%3.1f%%", i);
		wcscat_s(other, str);
		std::wstring v(other);
		m_pPanBar->setToolTipText(v.c_str());
		m_Pan=i;
		m_vPos.x=m_Pan;
		m_pChannel->set3DAttributes(&m_vPos,0);
	}

}
bool SampleBoard::EventHandler(const SEvent& event)
{
	s32 id = event.GUIEvent.Caller->getID()%m_IdRangeStart;

	switch(event.GUIEvent.EventType)
	{
	case EGET_FILE_SELECTED:
		switch(id)
		{
			case SAMPLE_FILE_OPEN:
		m_Path=m_pFileOpen->getFileName();
		if(m_Path!=NULL)
		{
			m_pChannel->stop();
			s32 i = m_Path.findLast('/');
			s32 l = m_Path.size()-i;
			m_wFile = m_Path.subString(i+1,l-1);
			m_pFileDisplay->setText(m_wFile.c_str());
			m_pSample = TheAudio::Instance()->LoadSample(m_Path.c_str(),m_SampleName,m_pSample);
			m_bPaused=false;
		}
		return true;
		}
		break;
	case EGET_BUTTON_CLICKED:
		switch(id)
		{
		case SAMPLE_MUTE_BUTTON:
			m_bMute=!m_bMute;
			m_pChannel->setMute(m_bMute);
			return true;

		case SAMPLE_LOAD_BUTTON:
			m_pFileOpen = TheEngine::Instance()->GetGUI()->addFileOpenDialog(L"Please choose a file.",1,0,m_IdRangeStart+SAMPLE_FILE_OPEN);
			return true;

		case SAMPLE_PLAY_BUTTON:
			if(!m_bPlaying)
			{
				m_pChannel = TheAudio::Instance()->PlaySample(m_pSample,m_pChannel);

				m_bPaused=false;
				if(m_bLooping)
				{
					m_pChannel->setLoopCount(-1);
				}
				else
				{
					m_pChannel->setLoopCount(0);
				}
				m_pChannel->setMute(m_bMute);
				m_pChannel->setVolume(m_Vol);
				m_pChannel->set3DAttributes(&m_vPos,0);
				m_pSample->setMusicSpeed(m_Speed);
				m_pChannel->getFrequency(&m_Freq);
				//m_pChannel->setFrequency(m_Pitch*m_Freq);
				m_pChannel->addDSP(m_pDsp1, 0);
				m_pChannel->addDSP(m_pDsp2, 0);
				m_pDsp1->setBypass(!m_bHPF);
				m_pDsp2->setBypass(!m_bLPF);
				m_pPitchShifter->setParameter(0,m_Pitch);
				/*m_pPitchBar->setPos(100);*/
				m_pChannel->addDSP(m_pPitchShifter, 0);
				m_pChannel->addDSP(m_pReverb, 0);
				m_pReverb->setParameter(0,m_Rev);
				m_pChannel->setPaused(false);
			}
			else
			{
				bool b;
				m_pChannel->getPaused(&b);
				m_pChannel->setPaused(!b);
				m_bPaused=!b;
			}
			return true;

		case SAMPLE_LOOP_BUTTON:
			m_bLooping=!m_bLooping;
			if(m_bLooping)
			{
				m_pChannel->setLoopCount(-1);
			}
			else
			{
				m_pChannel->setLoopCount(0);
			}
			return true;
		case SAMPLE_HPF_BUTTON:
			m_bHPF=!m_bHPF;
		
			m_pDsp1->setBypass(!m_bHPF);
			
			return true;
		case SAMPLE_LPF_BUTTON:
			m_bLPF=!m_bLPF;
			
			m_pDsp2->setBypass(!m_bLPF);
			
			return true;

		default:
			return false;
		}
		break;

	default:
		break;
	}
	return false;
}
void SampleBoard::Override(float dist, float rev)
{
	m_vPos.z=dist;
	m_pChannel->set3DAttributes(&m_vPos,0);
	m_pReverb->setParameter(0,rev);
}

void SampleBoard::Cleanup()
{
}